package com.jerry.server;

import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;


public class NioEndpoint extends AbstractEndpoint<SocketChannel> {
    private volatile ServerSocketChannel serverSock = null;

    private String address;

    private int port;
    //最大允许连接数
    private int acceptCount;

    public int getAcceptCount() {
        return acceptCount;
    }

    public void setAcceptCount(int acceptCount) {
        this.acceptCount = acceptCount;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public SocketChannel serverSocketAccept() throws Exception{
      return    serverSock.accept();
    }

    public void bind() throws Exception {
        initServerSocket();
    }

    public boolean setSocketOptions(SocketChannel socket) {
        return false;
    }

    public void closeSocket(SocketChannel socket) {

    }

    public void destroySocket(SocketChannel socket) {

    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    private void initServerSocket()throws Exception{

        serverSock = ServerSocketChannel.open();
        InetSocketAddress addr = new InetSocketAddress(getAddress(), getPort());
        serverSock.socket().bind(addr,getAcceptCount());



    }
}
